target = nindex

PLATFORM = POSIX64
#PLATFORM = WINDOWS64

ifeq ($(PLATFORM),WINDOWS64)
	CXX = x86_64-w64-mingw32-g++.exe
	CC = gcc
	CXXFLAGS = -Wall -m64 -O3 -mtune=corei7 -mfpmath=sse -mmmx -msse -msse2 -msse4.2 -DWINDOWS64
	INCL_DIRS =
	LDFLAGS = -g
	LDLIBS =
else
	CXX = g++
	CC = gcc
	CXXFLAGS = -Wall -m64 -O3 -mtune=corei7 -mfpmath=sse -mmmx -msse -msse2 -msse4.2 -DPOSIX64
	INCL_DIRS =
	LDFLAGS = -g -pthread
	LDLIBS =
endif


# construct lists of .cpp and their corresponding .o files
sources_cc = $(wildcard *.cc)
sources_asm = $(wildcard *.S)
sources = $(sources_cc) $(sources_asm)
objects = $(sources_cc:.cc=.o) $(sources_asm:.S=.o)
dependency = Makefile.dep

.PHONY : all clean
all : parser.cc $(target)
 
$(target) : $(objects)
	@echo linking...
	@$(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@

.cc.o :
	@echo compiling $<...
	@$(CXX) $(INCL_DIRS) -c $< $(CXXFLAGS) -o $@

.S.o :
	@echo compiling $<...
	@$(CXX) $(INCL_DIRS) -c $< $(CXXFLAGS) -o $@

parser.cc : parser.y ./lemon/lemon
	@echo compiling $<...
	@./lemon/lemon -q parser.y
	@mv parser.c parser.cc

./lemon/lemon: ./lemon/lemon.c
	@echo compiling lemon...
	@$(CC) $< -O2 -s -pipe -o $@

ifneq ($(MAKECMDGOALS), clean)
-include $(dependency)
endif

$(dependency) : parser.cc
	@echo 
	@echo generating dependencies...
	@$(RM) $(dependency)
	@$(CXX) -E -MM $(CXXFLAGS) $(sources) >> $(dependency)


clean :
	@$(RM) $(target) $(target).exe $(dependency) $(objects) parser.h parser.cc ./lemon/lemon ./lemon/lemon.exe
	@echo target, dependencies and objects were removed
